package org.colin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DEntry;

import org.colin.dictPojo.DEntryPhrase;
import org.colin.dictPojo.DSubject;
import org.colin.mapper.*;
import org.colin.dictPojo.DLanguage;
import org.colin.mapper.DSubjectMapper;
import org.colin.service.DLanguageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Service
public class DLanguageServiceImpl extends ServiceImpl<DLanguageMapper, DLanguage> implements DLanguageService {

    @Autowired
    DLanguageMapper dLanguageMapper;
    @Autowired
    DEntryMapper dEntryMapper;

    @Autowired
    DSubjectMapper dSubjectMapper;
    @Autowired
    DEntryImageMapper dEntryImageMapper;
    @Autowired
    DEntryVideoMapper dEntryVideoMapper;
    @Autowired
    DEntryPhraseMapper dEntryPhraseMapper;


    @Override
    @Transactional
    public boolean addLanguage(DLanguage dLanguage) {
        boolean save = false;
        try{
            dLanguage.setCreateTime(LocalDateTime.now());
            dLanguageMapper.insert(dLanguage);
            String newEntryTableName = ConfigCode.EntryTaleName+dLanguage.getId();
            String imageTableName = ConfigCode.EntryImageTaleName+dLanguage.getId();
            String videoTableName = ConfigCode.EntryVideoTaleName+dLanguage.getId();
            String phraseTableName = ConfigCode.EntryPhraseTaleName+dLanguage.getId();
            boolean saveEntryTable = dEntryMapper.createEntryTable(newEntryTableName);
            boolean saveEntryImageTable = dEntryImageMapper.createEntryImageTable(imageTableName);
            boolean saveEntryVideoTable = dEntryVideoMapper.createEntryVideoTable(videoTableName);
            boolean savePhraseTable = dEntryPhraseMapper.createEntryPhraseTable(phraseTableName);
            save = true;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return save;

        }
    }

    @Override
    @Transactional
    public Integer deleteLanguage(List<Integer> ids) {
        Integer update = 0;
        try{

            for (int i = 0; i < ids.size(); i++) {
                Integer id = ids.get(i);
                String tableName = ConfigCode.EntryTaleName+id;
                int i1 = dEntryMapper.selectCountByTableName(tableName);
                if(i1 > 0){
                    return  2 ;
                }
                QueryWrapper<DSubject> dSubjectQueryWrapper = new QueryWrapper<>();
                dSubjectQueryWrapper.eq("language" , id);
                Integer integer = dSubjectMapper.selectCount(dSubjectQueryWrapper);
                if(integer > 0){
                    return 3 ;
                }
                dLanguageMapper.deleteById(id);
            }
            update = 1;
        }catch (Exception e){
            update = 0 ;
            e.printStackTrace();
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            return update;

        }
    }
}
